
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_module/config/ServiceUlr.dart';
import 'package:flutter_module/view_model/query_wuliao.dart';
import 'package:flutter_module/widget/provider_widget.dart';
import 'package:flutter_module/widget/tap_appbar.dart';
import 'package:flutter_screenutil/screenutil.dart';

class ChuKuJiLu extends StatefulWidget {
  @override
  _ChuKuJiLu createState() => _ChuKuJiLu();
}

class _ChuKuJiLu extends State<ChuKuJiLu> {
  List<List> list = [];
  var map = {
    "1": "交通维护类",
    "2": "水管接头",
    "3": "水泵控制柜",
    "4": "工作服",
    "5": "安全救生",
    "6": "小工具",
    "7": "排水专用工具",
    "8": "其他",
    "9": "全部",
    "1001": "讯腾物料1",
    "1002": "讯腾物料2"
  };

  var buMen = {
    "1":"施工一分",
    "2":"防汛抢险分公司",
    "3":"施工三分",
    "4":"施工四分",
    "9":"公司",
    "5":"应急办",
    "8":"防汛库房",
    "EXDG":"二项代管",
    "6":"经营部",
    "10":"管网运营分公司",
    "7":"24小时值班室",
    "11":"施工二分",
    "12":"公司机关领导",
    "13":"工程管理中心",
    "14":"总工办",
    "SJZX":"水检中心",
    "GAT":"高安屯水厂",
  };

  var ling = {
    "1":"北辛店桥区",
    "2":"大鲁店抽水",
    "3":"大兴新机场（三项经手）",
    "4":"防汛物资发放表",
    "5":"干活用",
    "6":"公司安全演练",
    "7":"京沈泵站",
    "8":"京沈泵站防汛",
    "9":"抢险队干活用",
    "10":"抢险队用",
    "11":"抢险队用（张志刚批）",
    "12":"抢险队自用",
    "13":"损坏无法使用",
    "14":"无法使用报损",
    "15":"新学员用",
    "16":"演练",
    "17":"演练用",
    "18":"一带一路保障",
    "19":"值班室用",
    "20":"中水",
    "CNTEN-Y1":"测试原因1",
  };
  var map1 = {
    "materielType": "",
    "materielNo": "",
    "materielName": "",
    "pageIndex": "1",
    "pageSize": "10",
    "companyId": "402880185889ef6e015889f8c51e0000",
    "userId": "8a2a2a295c1ec767015c2ddd9d1e0001",
  };

  String value;
  var xmbValue;
  var gcztValue;
  var xzValue;
  String department;
  String receiveReason;
  String kuName = "";
  String kuNo;
  String materielName;
  var sgyyValue;
  var sgfeValue;
  var sDate = " ";
  var eDat = "";
  String materielNo = "";
  wuliaoQueryModel queryModel = new wuliaoQueryModel();

  @override
  void initState() {
    // TODO: implement initState
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
//      backgroundColor:Colors.white,
      appBar: TapAppBar(TapViewBar(Text("出库记录"), icon: Icon(Icons.arrow_back)),
          Size.fromHeight(ScreenUtil().setHeight(80))),
      body: ProviderWidget<wuliaoQueryModel>(
        model: wuliaoQueryModel(),
        onModelInit: (model) {
          model.url = "queryMateriels";
          model
              .getHttpData(servicePath["stockOutputRecord"], data: map1)
              .then((value) {
            list = model.data;
          });
        },
        builder: (context, model, child) {
          return EasyRefresh(
              controller: model.controller,
              footer: MaterialFooter(),
              enableControlFinishLoad: true,
              onLoad: model.onLoad,
              bottomBouncing: true,
              child: SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    getInPutName1(),

                    SizedBox(
                      height: 1,
                      width: ScreenUtil().setWidth(700),
                      child: Container(
                        color: Colors.black12,
                      ),
                    ),
                    Container(
                      color: Colors.white,
                      child: getDate2("开始时间"),
                    ),
                    SizedBox(
                      height: 1,
                      width: ScreenUtil().setWidth(700),
                      child: Container(
                        color: Colors.black12,
                      ),
                    ),
                    Container(
                      color: Colors.white,
                      child: getDate2("结束时间"),
                    ),
                    SizedBox(
                      height: 1,
                      width: ScreenUtil().setWidth(700),
                      child: Container(
                        color: Colors.black12,
                      ),
                    ),
                    getItem("领用部门", buMen),
                    SizedBox(
                      height: 1,
                      width: ScreenUtil().setWidth(700),
                      child: Container(
                        color: Colors.black12,
                      ),
                    ),
                    getItem1("领用原因", ling),
                    SizedBox(
                      height: 1,
                      width: ScreenUtil().setWidth(700),
                      child: Container(
                        color: Colors.black12,
                      ),
                    ),
                    getQuery(model),
                    getWrap(),
                  ],
                ),
              ));
        },
      ),
    );
  }

  List<Widget> getListItem() {
    return list.map((value) {
      return Container(
        margin: EdgeInsets.only(left: 10, right: 10, top: 10),
        padding: EdgeInsets.only(left: 20, top: 10),
        decoration: new BoxDecoration(
          //背景
          color: Colors.white,
          //设置四周圆角 角度
          borderRadius: BorderRadius.all(Radius.circular(4.0)),
          //设置四周边框
//          border: new Border.all(width: 1, color: Colors.red),
        ),
//        color: Colors.white,
        child: Column(
          children: <Widget>[
            getListItemData("出库单号:", value[0]),
            SizedBox(
              height: 10,
            ),
            getListItemData("出库时间:", value[1]),
            SizedBox(
              height: 10,
            ),
            getListItemData("类        别:", value[2]),
            SizedBox(
              height: 10,
            ),
            getListItemData("物料名称:", value[3]),
            SizedBox(
              height: 10,
            ),
            getListItemData("规        格:", value[4]),
            SizedBox(
              height: 10,
            ),
            getListItemData("计量单位:", value[5]),
            SizedBox(
              height: 10,
            ),
            getListItemData("数        量:", value[6]),
            SizedBox(
              height: 10,
            ),
            getListItemData("存房名称:", value[7]),
            SizedBox(
              height: 10,
            ),
            getListItemData("领用部门:", value[8]),
            SizedBox(
              height: 10,
            ),
            getListItemData("领  用  人:", value[9]),
            SizedBox(
              height: 10,
            ),
            getListItemData("领用原因:", value[10]),
            SizedBox(
              height: 10,
            ),
            getListItemData("备        注:", value[11]),
            SizedBox(
              height: 10,
            ),
          ],
        ),
      );
    }).toList();
  }

  Widget getListItemData(String name, String data) {
    return Row(
      children: <Widget>[
        Expanded(
          child: Text(
            name,
            style: TextStyle(fontWeight: FontWeight.bold),
          ),
          flex: 1,
        ),
        Expanded(
          child: Text(
            data,
            style: TextStyle(
                color: name == "状        态:" ? Colors.red : Colors.black54),
          ),
          flex: 4,
        )
      ],
    );
  }

  Widget getDownButton(Map<String, String> list) {
    var list1 = List<DropdownMenuItem>();
    list.forEach((String key, String value) {
      list1.add(DropdownMenuItem(child: Text(value), value: key));
    });
    return DropdownButtonHideUnderline(
        child: DropdownButton(
            items: list1,
            value: department,
            onChanged: (value) {
              setState(() {
                this.department = value;
              });
            }));
  }

  Widget getDownButton1(Map<String, String> list) {
    var list1 = List<DropdownMenuItem>();
    list.forEach((String key, String value) {
      list1.add(DropdownMenuItem(child: Text(value), value: key));
    });
    return DropdownButtonHideUnderline(
        child: DropdownButton(
            items: list1,
            value: receiveReason,
            onChanged: (value) {
              setState(() {
                this.receiveReason = value;
              });
            }));
  }

  Widget getItem(String title, Map<String, String> list) {
    return Container(
      color: Colors.white,
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(80),
      padding: EdgeInsets.only(left: 30, right: 30),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text(title),
            flex: 1,
          ),
          Expanded(
            child: getDownButton(list),
            flex: 2,
          )
        ],
      ),
    );
  }

  Widget getItem1(String title, Map<String, String> list) {
    return Container(
      color: Colors.white,
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(80),
      padding: EdgeInsets.only(left: 30, right: 30),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text(title),
            flex: 1,
          ),
          Expanded(
            child: getDownButton1(list),
            flex: 2,
          )
        ],
      ),
    );
  }


  Widget getInPutName1() {
    return Container(
      color: Colors.white,
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(80),
      padding: EdgeInsets.only(left: 30, right: 30),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text("出库单号"),
            flex: 1,
          ),
          Expanded(
            child: TextField(
              decoration: InputDecoration(
                //  labelText: sp.getUserInfo("num").toString(),
                fillColor: Colors.white,
                filled: true,
                border: InputBorder.none,
                hintText: "",
              ),
              onChanged: (value) {
                materielName = value;
              },
            ),
            flex: 2,
          )
        ],
      ),
    );
  }

  Widget getQuery(wuliaoQueryModel model) {
    return Container(
      // color: Colors.black12,
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(80),
      margin: EdgeInsets.only(top: 10),
      padding: EdgeInsets.only(left: 30, right: 30),
      child: Row(
        children: <Widget>[
          Expanded(
            child: RaisedButton(
              onPressed: () {

//                stockOutputNo:
//                minStockOutputDate:
//                maxStockOutputDate:
//                department:
//                receiveReason:
                var map = {
                  "stockOutputNo": this.materielName,
                  "minStockOutputDate": this.sDate,
                  "maxStockOutputDate": this.eDat,
                  "department":this.department,
                  "receiveReason":this.receiveReason,
                  "companyId": "402880185889ef6e015889f8c51e0000",
                  "userId": "8a2a2a295c1ec767015c2ddd9d1e0001",
                };
                model.map = map;
                model.query = false;
                model.getHttpData(servicePath["stockOutputRecord"], data: map);
//                model
//                    .getHttpData(servicePath["queryMateriels"], data:map1)
//                    .then((value) {
//                  setState(() {
//                    this.list.clear();
//                    model.query = false;
//                    list.addAll(value);
//                  });
//                });
              },
              child: Text("查询"),
              textColor: Colors.white,
              color: Theme.of(context).primaryColor,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8),
                // side: BorderSide(color: Colors.red)
              ),
            ),
            flex: 1,
          ),
//          Text("   "),
        ],
      ),
    );
  }

  Widget getWrap() {
    if (list == null) {
      return Text(" ");
    } else {
      return Container(
//        padding: EdgeInsets.only(left: 30),
        child: Wrap(
          spacing: 1,
          children: getListItem(),
        ),
      );
    }
  }

  Widget getDate(String sDate) {
    return Container(
      height: ScreenUtil().setHeight(80),
      child: FlatButton(
          onPressed: () {
            DatePicker.showDatePicker(context,
                showTitleActions: true,
                minTime: DateTime(2010, 3, 5),
                maxTime: DateTime(2030, 6, 7),
                onChanged: (date) {}, onConfirm: (date) {
                  setState(() {
                    this.sDate = date.toString().substring(0, 10);
                  });
                }, currentTime: DateTime.now(), locale: LocaleType.zh);
          },
          child: Container(
            alignment: Alignment.center,
            color: Colors.white,

            padding: EdgeInsets.only(left: 15, right: 30),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Text(sDate),
                  flex: 1,
                ),
                Expanded(
                  child: Text(this.sDate),
                  flex: 2,
                )
              ],
            ),
          )),
    );
  }

  Widget getDate2(String eDate) {
    return Container(
      height: ScreenUtil().setHeight(80),
      child: FlatButton(
          onPressed: () {
            DatePicker.showDatePicker(context,
                showTitleActions: true,
                minTime: DateTime(2010, 3, 5),
                maxTime: DateTime(2030, 6, 7),
                onChanged: (date) {}, onConfirm: (date) {
                  setState(() {
                    this.eDat = date.toString().substring(0, 10);
                  });
                }, currentTime: DateTime.now(), locale: LocaleType.zh);
          },
          child: Container(
            alignment: Alignment.center,
            color: Colors.white,

            padding: EdgeInsets.only(left: 15, right: 30),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Text(eDate),
                  flex: 1,
                ),
                Expanded(
                  child: Text(this.eDat),
                  flex: 2,
                )
              ],
            ),
          )),
    );
  }
}
